package com.huijie.core.interceptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.huijie.app.util.ContextHelper;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 *  注入公共字段自动填充,任选注入方式即可
 */
//@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    protected final static Logger logger = LoggerFactory.getLogger(MyMetaObjectHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
//        logger.info("新增的时候干点不可描述的事情");
        HttpServletRequest currRequest = ContextHelper.getCurrRequest();
        if(currRequest!=null){
            this.setFieldValByName("creator", String.valueOf(ContextHelper.getUserIdByRequest(currRequest)), metaObject);
        }
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("deleted", 0, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
//        logger.info("更新的时候干点不可描述的事情");
        HttpServletRequest currRequest = ContextHelper.getCurrRequest();
        if(currRequest!=null){
            this.setFieldValByName("modifier", String.valueOf(ContextHelper.getUserIdByRequest(currRequest)), metaObject);
        }
        this.setFieldValByName("modifyTime", new Date(), metaObject);
    }
}
